package com.ryan.dispatcher;

import com.ryan.dispatcher.ability.ArgsAbility;
import com.ryan.ohos.extension.util.WindowUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability {

    private SampleProvider provider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        WindowUtil.hideStatusBar(getWindow(), true);

        setUIContent(ResourceTable.Layout_ability_main);

        initList();
    }

    private List<Menu> createMenu() {
        List<Menu> menus = new ArrayList<>();

        menus.add(new Menu("触摸事件", null, null));
        menus.add(new Menu("事件分发", ArgsAbility.class, Args.ARG_TOUCH_EVENT));
        menus.add(new Menu("事件冲突", ArgsAbility.class, Args.ARG_EVENT_CONFLICT));
        menus.add(new Menu("下拉刷新", ArgsAbility.class, Args.ARG_PULL_TO_REFRESH));

        menus.add(new Menu("NestedScrolling", null, null));
        menus.add(new Menu("基础用法", ArgsAbility.class, Args.ARG_NESTED_SCROLLING));
        menus.add(new Menu("CoordinatorLayout、AppBarLayout、Behavior", ArgsAbility.class, Args.ARG_COORDINATOR));
        menus.add(new Menu("CoordinatorLayout、NestedListContainer", ArgsAbility.class, Args.ARG_COORDINATOR_LIST));
        menus.add(new Menu("CoordinatorLayout、PageSlider、ListContainer", ArgsAbility.class, Args.ARG_COORDINATOR_SLIDER_LIST));
        menus.add(new Menu("DrawerLayout", ArgsAbility.class, Args.ARG_DRAWER_LAYOUT));


        menus.add(new Menu("手势识别", null, null));
        menus.add(new Menu("GestureDetector", ArgsAbility.class, Args.ARG_GESTURE_DETECTOR));
        menus.add(new Menu("ScaleGestureDetector", ArgsAbility.class, Args.ARG_SCALE_GESTURE_DETECTOR));
        menus.add(new Menu("图片缩放", ArgsAbility.class, Args.ARG_IMAGE_SCALING));

        menus.add(new Menu("其他", null, null));
        menus.add(new Menu("Scroller", ArgsAbility.class, Args.ARG_SCROLLER));
        menus.add(new Menu("ScrollHelper", ArgsAbility.class, Args.ARG_SCROLL_HELPER));
        menus.add(new Menu("TouchEventCompact", ArgsAbility.class, Args.ARG_TOUCH_EVENT_COMPACT));

        return menus;
    }

    private void initList() {
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_container);
        provider = new SampleProvider(createMenu());
        container.setItemProvider(provider);

        container.setItemClickedListener((list, component, position, id) -> {
            Menu menu = provider.getItem(position);
            if (menu.ability == null) return;
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getBundleName())
                    .withAbilityName(menu.ability.getCanonicalName())
                    .build();

            Intent intent = new Intent();
            IntentParams intentParams = new IntentParams();
            intentParams.setParam(Args.NAME, menu.text);
            intentParams.setParam(Args.ARG_KEY, menu.arg);

            intent.setParams(intentParams);
            intent.setOperation(operation);
            startAbility(intent);
        });
    }

    class SampleProvider extends BaseItemProvider {

        private final List<Menu> data;

        public SampleProvider(List<Menu> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Menu getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convert, ComponentContainer container) {
            final Component cpt;
            if (convert == null) {
                cpt = LayoutScatter.getInstance(MainAbility.this).parse(ResourceTable.Layout_item_main, null, false);
            } else {
                cpt = convert;
            }

            Text title = (Text) cpt.findComponentById(ResourceTable.Id_item_title);
            Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_text);
            Menu menu = data.get(position);
            if (menu.ability == null) {
                title.setText(data.get(position).text);
            } else {
                text.setText(data.get(position).text);
            }
            title.setVisibility(menu.ability == null ? Component.VISIBLE : Component.HIDE);
            text.setVisibility(menu.ability == null ? Component.HIDE : Component.VISIBLE);
            return cpt;
        }
    }

    static class Menu {
        String text;
        Class<?> ability;
        String arg;

        public Menu(String text, Class<?> ability, String arg) {
            this.text = text;
            this.ability = ability;
            this.arg = arg;
        }
    }
}
